﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace SigowayCommonLib
{
    public class LogManager
    {
        private static StreamWriter swLogger = null;
       //test sourcetree
        public static void CreateLogger(string logName)
        {
            string strLogPath = Path.Combine(Application.StartupPath, "Log\\" + logName);
            if (!Directory.Exists(strLogPath))
            {
                Directory.CreateDirectory(strLogPath);
            }

            if (swLogger == null)
            {
                swLogger = new StreamWriter(Path.Combine(strLogPath,
                        string.Format("{0}_{1:yyyyMMddHHmmssfff}.log", logName, DateTime.Now)), true);
                swLogger.AutoFlush = true; 
            }
        }

        public static void CloseLogger()
        {
            if (swLogger != null)
            {
                swLogger.Flush();
                swLogger.Close(); 
            }
        }

        public static void WriteLine(string logText)
        {
            if (swLogger == null)
            {
                CreateLogger(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
            }

            swLogger.WriteLine("[{0:yyyy/MM/dd HH:mm:ss:fff}] [{1}]", DateTime.Now, logText);

        }

        public static void WriteLine(string format, params object[] args)
        {
            if (swLogger == null)
            {
                CreateLogger(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
            }

            swLogger.WriteLine("[{0:yyyy/MM/dd HH:mm:ss:fff}] [{1}]", DateTime.Now, string.Format(format, args));
        }
    }
}
